package kata14;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WordAssemble02 {
    private List<String> sourceWords;

    private Map<String,List<String>> wordDictionary;


    public WordAssemble02(){
        sourceWords = new ArrayList<>();
        wordDictionary = new HashMap<>();
    }

    //1.读取文件 到单词列表
    public void  readFile(String path){
        try(BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(path)))){
            String strLine = "";
            List<String> splitList;
            while ((strLine = br.readLine()) != null){
                //分割字符串
                splitList = Stream.of(strLine.trim().split(" "))
                        //字符串不能为空,否则将其从列表中移除
                        .filter(word -> word != null && word.length() > 0)
                        .collect(Collectors.toList());
                sourceWords.addAll(splitList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //2.简单的单词处理
    public void simpleWordProcess(){
        int arraySize = sourceWords.size();
        if(arraySize >= 3){
            arraySize = arraySize -2;
            String key = null;
            List<String> mapInnerList;
            for(int i = 0; i < arraySize; i++){
                key = sourceWords.get(i).concat(" ").concat(sourceWords.get(i+1));
                mapInnerList = wordDictionary.get(key);
                if(mapInnerList == null){
                    mapInnerList = new ArrayList<>();
                }
                mapInnerList.add(sourceWords.get(i+2));
                wordDictionary.put(key,mapInnerList);
            }
        }
    }

    //3.打印生成的单词库
    public void print(){
        for (Map.Entry<String,List<String>> entry: wordDictionary.entrySet()) {
            System.out.println(entry.getKey().concat(" => ").concat(entry.getValue().toString()));
        }
    }

    //4.输入两个单词(必须使用单词库中的单词)  匹配单词库中可生成的句子
    public String genericWordLine(String words){
        if(wordDictionary.get(words) == null){
            throw new RuntimeException("单词库中不存在输入的单词");
        }
        //打印的句子 的单词存储列表
        List<String> resultList = Stream.of(words.split(" ")).collect(Collectors.toList());

        boolean isExists = true; //下一个单词是否查找到
        String nextWord; //下一个单词
        String middle; //中间单词
        while (isExists){
            middle = resultList.get(resultList.size() - 2).concat(" ").concat(resultList.get(resultList.size() - 1));
            nextWord = getRandomNextWord(middle);
            //当单词为空,则跳出循环
            if(nextWord == null){
                isExists = false;
            }else {
                //加入单词列表
                resultList.add(nextWord);
            }
        }

        //将单词列表返回成句子
        return resultList.stream().collect(Collectors.joining(" "));
    }

    //4.1 获取下一个单词
    private String getRandomNextWord(String nextWord){
        List<String> innerWords = wordDictionary.get(nextWord);
        if(innerWords == null || innerWords.size() == 0){
            return null;
        }
        Random random = new Random();
        //随机数的生成范围在  innerWords.size() 的区间
        //从数组中,获取一个单词
        return innerWords.get(random.nextInt(innerWords.size()));
    }


    public static void main(String[] args) {
        WordAssemble02 wordAssemble = new WordAssemble02();
        //模拟写入单词
        //I wish I may I wish I might
        wordAssemble.sourceWords.add("I");
        wordAssemble.sourceWords.add("wish");
        wordAssemble.sourceWords.add("I");
        wordAssemble.sourceWords.add("may");
        wordAssemble.sourceWords.add("I");
        wordAssemble.sourceWords.add("wish");
        wordAssemble.sourceWords.add("I");
        wordAssemble.sourceWords.add("might");

        //单词处理
        wordAssemble.simpleWordProcess();
        //打印
        wordAssemble.print();

        System.out.println("================ generic word line =============");
        //生成句子
        String result = wordAssemble.genericWordLine("I may");
        System.out.println(result);
    }
}
